package com.kangaroo.myhome.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.kangaroo.myhome.mapper.TSysOptionMapper;
import com.kangaroo.myhome.model.*;
import com.kangaroo.myhome.utils.SecurityUtils;
import com.kangaroo.myhome.vo.Option;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kangaroo.myhome.mapper.TSysMenuMapper;
import com.kangaroo.myhome.model.TSysOption;
import com.kangaroo.myhome.vo.ResponseModel;

import tk.mybatis.mapper.util.StringUtil;

@Service
public class TSysOptionService {

    @Autowired
    private TSysOptionMapper tSysOptionMapper;

    /**
     *
     * @param opName
     * @param opCode
     * @param page
     * @param rows
     * @return
     */
    public ResponseModel findAll(String opName,String opCode,String pId, int page, int rows) {
        ResponseModel model = new ResponseModel();

        CommonSearch search = new CommonSearch();
        search.setString1(opName);
        search.setString2(opCode);
        search.setString3(pId);
        search.setPagination(page, rows);
        List<TSysOption> users = tSysOptionMapper.selectByPagination(search);
        long count = tSysOptionMapper.selectByPaginationCount(search);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(users, count);
        return model;
    }

    public ResponseModel findByCode(String opCode) {
        ResponseModel model = new ResponseModel();

        List<TSysOption> users = tSysOptionMapper.findByCode(opCode);
        List<Option> options = new ArrayList<Option>();
        for(TSysOption subway:users){
            Option option = new Option(subway.getOpCode(),subway.getOpName());
            options.add(option);
        }

        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setData(options);
        return model;
    }

    /**
     * @param tSysOption
     * @return
     */
    public ResponseModel edit(TSysOption tSysOption) {

        ResponseModel model = new ResponseModel();

        if (tSysOption == null) {
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }

        int result = 0;
        String id = tSysOption.getId();
        if (StringUtil.isEmpty(id)) {
            TSysOption subWayExist = view(tSysOption);
            if (subWayExist != null) {
                model.setCode(201);
                model.setMessage("数字字典：【" + tSysOption.getOpName() + "】已经存在！");
                return model;
            } else {
                tSysOption.setCreateTime(new Date());
                tSysOption.setId(UUID.randomUUID().toString());
                tSysOption.setCreator(SecurityUtils.getUsername());
                result = tSysOptionMapper.insertSelective(tSysOption);
            }

        } else {
            result = tSysOptionMapper.updateByPrimaryKeySelective(tSysOption);
        }

        model.setCode(200);
        model.setMessage("操作成功");
        return model;
    }

    /**
     * @param tSysOption
     * @return
     */
    public TSysOption view(TSysOption tSysOption) {
        ResponseModel model = new ResponseModel();

        TSysOptionExample example = new TSysOptionExample();
        String id = tSysOption.getId();
        String opCode = tSysOption.getOpCode();
        TSysOption resultOption = null;
        if (StringUtil.isNotEmpty(id)) {
            resultOption = tSysOptionMapper.selectByPrimaryKey(id);
            return resultOption;
        }
        if (StringUtil.isNotEmpty(opCode)) {
            example.createCriteria().andOpCodeEqualTo(opCode);
            List<TSysOption> users = tSysOptionMapper.selectByExample(example);
            if (users != null && users.size() > 0) {
                resultOption = users.get(0);
            }
        }

        return resultOption;
    }

    public void delete(TSysOption tSysOption) {
        tSysOptionMapper.deleteByPrimaryKey(tSysOption.getId());
        TSysOptionExample option = new TSysOptionExample();
        option.createCriteria().andPIdEqualTo(tSysOption.getId());
        tSysOptionMapper.deleteByExample(option);

    }

}
